import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import WorkTable from '../views/WorkTable.vue'
import TuiJian from '../views/TuiJian.vue'

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: HomeView,
        children: [
            {
                path: '/home',
                redirect: '/home/worktable'
            },
            {
                path: '/home/worktable',
                name: 'worktable',
                component: WorkTable
            },
            {
                path: 'tuijian',
                name: 'tuijian',
                component: TuiJian
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/template',
        name: 'template',
        component: () => import('../views/Template.vue')
    },
    {
        path: '/:catchAll(.*)',
        name: '页面丢失',
        component: () => import('../views/NotFinnd.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
    // 获取本地token
    const token = localStorage.getItem('token')
    if (!token && to.name !== 'login') {
        next('/login')
    } else {
        next()
    }
})

export default router;